package top.codecrab.eblog.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.codecrab.eblog.common.response.R;
import top.codecrab.eblog.config.Constants;
import top.codecrab.eblog.entity.Category;
import top.codecrab.eblog.entity.Comment;
import top.codecrab.eblog.entity.Post;
import top.codecrab.eblog.entity.UserMessage;
import top.codecrab.eblog.service.*;
import top.codecrab.eblog.utils.RedisUtil;

/**
 * @author codecrab
 */
@Slf4j
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private PostService postService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserMessageService messageService;

    @Autowired
    private UserCollectionService collectionService;

    @Autowired
    private CategoryService categoryService;

//    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R jieSet(Long id, String field, Integer rank) {
        Post post = postService.getById(id);
        Assert.notNull(post, "该文章已被封禁或删除");

        switch (field) {
            case Constants.DELETE:
                //删除 -- 逻辑删除
                postService.removeById(id);
                //更新每周热议
                redisUtil.zSet("week:hot:post", id, 0);
                redisUtil.del("week:hot:postInfo:" + id);

                //该分类下文章个数减1
                Category category = categoryService.getById(post.getCategoryId());
                Assert.notNull(category, "找不到分类");
                category.setPostCount(Math.max(category.getPostCount() - 1, 0));
                categoryService.updateById(category);

                //发送消息到队列
                // amqpTemplate.convertAndSend(MqTypes.POST_REMOVE_ROUTING_KEY, id);

                //删除关联的收藏
                collectionService.removeByMap(MapUtil.of("post_id", id));
                //逻辑删除评论和消息
                commentService.remove(new QueryWrapper<Comment>().eq("post_id", id));
                messageService.remove(new QueryWrapper<UserMessage>().eq("post_id", id));

                // 重置本周最热
                postService.resetWeekHot(id);
                postService.updateById(post);
                return R.ok().action("/user/index");
            case Constants.STICK:
                //置顶
                post.setLevel(rank);
                postService.updateById(post);
                break;
            case Constants.STATUS:
                //加精
                post.setRecommend(rank == 1);
                postService.updateById(post);
                break;
            default:
                log.error("未知的操作类型");
                return R.ok();
        }

        return R.ok();
    }
}
